//
//  CountDownControl.m
//  ZKGuanjia
//
//  Created by Deve Xu on 16/1/26.
//  Copyright © 2016年 Deve Xu. All rights reserved.
//

#import "CountDownControl.h"

@interface CountDownControl () {
    NSInteger countNumber;;
    NSTimer *timer;
}

@end

@implementation CountDownControl

-(void)start:(NSInteger)count {
    _countLabel.text = [NSString stringWithFormat:@"%@ %ld %@",@"剩余",count,@"秒"];
    _countLabel.hidden = NO;
    _operationView.hidden = YES;
    countNumber = count;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
}

-(void) start:(NSInteger)count withPrefix:(NSString *)prefix{
    _countLabel.text = [NSString stringWithFormat:@"%@（%ld）",@"重新发送",count];
    _countLabel.hidden = NO;
    _operationView.hidden = YES;
    countNumber = count;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
}

-(void)timeFireMethod {
    countNumber--;
    [self performSelectorOnMainThread:@selector(updateText) withObject:nil waitUntilDone:NO];
    if (countNumber == 0) {
        [timer invalidate];
    }
}

-(void)updateText {
    if (countNumber == 0) {
        _countLabel.hidden = YES;
        _operationView.hidden = NO;
    } else {
        _countLabel.text = [NSString stringWithFormat:@"%@ %ld %@",@"剩余", countNumber,@"秒"];
    }
}

@end
